package com.junzhiit.nengji.cms.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.junzhiit.nengji.cms.service.LogisticsService;
import com.junzhiit.nengji.cms.web.exception.CodeIsExistException;
import com.junzhiit.nengji.cms.web.exception.LogisticsNotFoundException;
import com.junzhiit.nengji.cms.web.exception.LogisticsNumberErrorException;
import com.junzhiit.nengji.cms.web.exception.NameIsExistException;
import com.junzhiit.nengji.cms.web.util.ViewObjectUtil;
import com.junzhiit.nengji.cms.web.util.aliyun.LogisticsUtil;
import com.junzhiit.nengji.cms.web.util.aliyun.bean.LogisticsResultDate;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.LogisticsAddVO;
import com.junzhiit.nengji.cms.web.vo.LogisticsEditVO;
import com.junzhiit.nengji.cms.web.vo.LogisticsVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.dao.LogisticsDAO;
import com.junzhiit.nengji.model.Logistics;

@Service
public class LogisticsServiceImpl extends BaseServiceImpl implements LogisticsService {

	@Autowired
	private LogisticsDAO logisticsDAO;

	@Override
	public Map<String, Object> findAll(DataTablesParamVO dtp, LogisticsVO logisticsVO) {
		Logistics logistics = new Logistics();
		BeanUtils.copyProperties(logisticsVO, logistics);
		List<Logistics> list = logisticsDAO.findAll(dtp.getStart(), dtp.getLength(), logistics);

		List<LogisticsVO> listVO = new ArrayList<LogisticsVO>();
		for (Logistics param : list) {
			LogisticsVO vo = new LogisticsVO();
			BeanUtils.copyProperties(param, vo);
			listVO.add(vo);
		}
		
		int totalCount = logisticsDAO.getTotalCount(logistics);
		return ViewObjectUtil.dataTables(listVO, dtp.getDraw(), totalCount);
	}

	@Override
	public ResultVO findById(String id) {
		ResultVO resultVO = new ResultVO();
		Logistics logistics = logisticsDAO.query(id);
		if (logistics == null) {
			throw new LogisticsNotFoundException("该物流不存在！");
		}
		LogisticsVO logisticsVO = new LogisticsVO();
		BeanUtils.copyProperties(logistics, logisticsVO);
		resultVO.setData(logisticsVO);
		return resultVO;
	}

	@Override
	public ResultVO add(LogisticsAddVO logisticsAddVO) {
		Logistics logistics = new Logistics();
		BeanUtils.copyProperties(logisticsAddVO, logistics);
		ResultVO resultVO = new ResultVO();
		if (logisticsDAO.getNameExist(logistics.getName()) > 0) {
			throw new NameIsExistException("物流名称已经存在");
		}
		if (logisticsDAO.getCodeExist(logistics.getCode()) > 0) {
			throw new CodeIsExistException("物流编码已经存在");
		}
		Logistics param = logisticsDAO.insert(logistics);
		LogisticsVO logisticsVO = new LogisticsVO();
		BeanUtils.copyProperties(param, logisticsVO);
		resultVO.setData(logisticsVO);
		return resultVO;
	}

	@Override
	public ResultVO delete(String id) {
		ResultVO resultVO = new ResultVO();
		try {
			resultVO.setData(logisticsDAO.updateToDelete(id));
		} catch (Exception e) {
			throw new LogisticsNotFoundException("该物流不存在！");
		}
		return resultVO;
	}

	@Override
	public ResultVO edit(LogisticsEditVO logisticsEditVO) {
		ResultVO resultVO = new ResultVO();
		Logistics logistics = logisticsDAO.query(logisticsEditVO.getId());
		if (logistics == null) {
			throw new LogisticsNotFoundException("该物流不存在！");
		}
		if (!logistics.getName().equals(logisticsEditVO.getName())) {
			if (logisticsDAO.getNameExist(logisticsEditVO.getName()) > 0) {
				throw new NameIsExistException("物流名称已经存在");
			}
		}
		if (!logistics.getCode().equals(logisticsEditVO.getCode())) {
			if (logisticsDAO.getCodeExist(logisticsEditVO.getCode()) > 0) {
				throw new CodeIsExistException("物流编码已经存在");
			}
		}
		BeanUtils.copyProperties(logisticsEditVO, logistics);
		logistics.setUpdatedTime(new Date());
		resultVO.setData(logisticsDAO.update(logistics));
		return resultVO;
	}

	@Override
	public ResultVO toTop(String id) {
		ResultVO resultVO = new ResultVO();
		Logistics logistics = logisticsDAO.query(id);
		if (logistics == null) {
			throw new LogisticsNotFoundException("该物流不存在！");
		}
		logistics.setSort(logisticsDAO.getMaxSort() + 1);
		logistics.setUpdatedTime(new Date());
		resultVO.setData(logisticsDAO.update(logistics));
		return resultVO;
	}

	@Override
	public ResultVO getList() {
		ResultVO result = new ResultVO();
		result.setData(logisticsDAO.getList());
		return result;
	}

	@Override
	public ResultVO logisticsTrace(String logisticsNumber,String logisticsCode) {
		ResultVO result =new ResultVO();
		LogisticsResultDate logisticsResultDate = LogisticsUtil.expressQuery(logisticsNumber, logisticsCode);
		if(logisticsResultDate == null){
			throw new LogisticsNumberErrorException("未查询到该物流单号");
		}
		result.setData(logisticsResultDate);
		return result;
	}

}
